package org.hbgl.geoentity.admin.web.unit;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hbgl.geoentity.admin.annotation.RequiresPermissionsDesc;
import org.hbgl.geoentity.admin.web.BaseController;
import org.hbgl.geoentity.core.util.ResponseUtil;
import org.hbgl.geoentity.db.domain.GeColumn;
import org.hbgl.geoentity.db.domain.GeUser;
import org.hbgl.geoentity.db.domain.SiteSetting;
import org.hbgl.geoentity.db.util.EnumFile;
import org.hbgl.geoentity.db.vo.CarouselVo;
import org.hbgl.geoentity.db.vo.CoVo;
import org.hbgl.geoentity.db.vo.ContentVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotNull;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 代码功能：授权信息
 * 开发日期：2022/4/22
 * 作者：蒋海仑
 * 联系方式：
 * 版本更迭说明：
 * 版权信息：河北广联信息技术有限公司
 */
@RestController
@RequestMapping("/unit/home")
@Validated
public class WebSiteController extends BaseController {

    /**
     * 获取顶级栏目
     * 顶级栏目 parentId为0
     */
    @GetMapping("/loginState")
    public Object loginState() {
        GeUser currentUnitUser = getCurrentUnitUser();
        return ResponseUtil.ok(currentUnitUser.getRealname());
    }

    /**
     * 获取顶级栏目
     * 顶级栏目 parentId为0
     */
    @GetMapping("/topColumnList")
    public Object topColumnList(@RequestParam(defaultValue = "0") Integer parentId) {
        // GeUnit userCurrent = getCurrentUnitUser();
        List<GeColumn> columnChildList = columnService.selectColumnByParentId(parentId);
        if(columnChildList != null && columnChildList.size()>0) {
            columnChildList = columnChildList.stream().filter(p->p.getState().equals(EnumFile.CommonState.Normal)).collect(Collectors.toList());
        }
        return ResponseUtil.ok(columnChildList);
    }

    /**
     * 通过父级栏目获取子栏目列表
     * 顶级栏目 parentId为0
     */
    @GetMapping("/parentColumnList")
    public Object parentColumnList(@RequestParam(defaultValue = "0") Integer parentId) {
        List<GeColumn> columnChildList = columnService.selectColumnByParentId(parentId);
        if(columnChildList != null && columnChildList.size()>0) {
            columnChildList = columnChildList.stream().filter(p->p.getState().equals(EnumFile.CommonState.Normal)).collect(Collectors.toList());
        }
        return ResponseUtil.ok(columnChildList);
    }

    /**
     * 通过栏目id获取内容列表
     */
    @GetMapping("/list")
    public Object list(@NotNull Integer columnId,
                       @RequestParam(defaultValue = "") String keywords,
                       @RequestParam(defaultValue = "") String startTime,
                       @RequestParam(defaultValue = "") String endTime,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit) {
        // GeUnit userCurrent = getCurrentUnitUser();
        if(StringUtils.isNotBlank(startTime)) {
            startTime += " 00:00:00";
        }
        if(StringUtils.isNotBlank(endTime)) {
            endTime += " 23:59:59";
        }
        List<ContentVo> contentList = contentService.selectSiteContentList(columnId, keywords, startTime, endTime);
        if(contentList != null && contentList.size()>0) {
            contentList = contentList.stream().filter(p->p.getState().equals(EnumFile.CommonState.Normal)).collect(Collectors.toList());
            if(contentList != null && contentList.size()>0) {
                contentList = contentList.stream().sorted(Comparator.comparing(ContentVo::getSort)).collect(Collectors.toList());
            }
        }
        return ResponseUtil.ok(contentList);
    }
    /**
     * 获取验证码
     */
    @GetMapping("/getkey")
    public Object getkey() {
        HttpSession session = request.getSession();
        String kaptcha = (String) session.getAttribute("kaptcha");
        if (kaptcha != null) {
            return ResponseUtil.ok(kaptcha);
        }
        return ResponseUtil.fail();
    }
    /**
     * 通过内容id获取内容详情
     */
    @GetMapping("/contentdetail")
    public Object contentDetail(@NotNull Integer id) {
        // GeUnit userCurrent = getCurrentUnitUser();
        ContentVo contentVo = contentService.findContentById(id);
        return ResponseUtil.ok(contentVo);
    }

    /**
     * 获取合作组织列表
     */
    @GetMapping("/colist")
    public Object colist(@RequestParam(defaultValue = "") String keywords,
                         @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer limit) {
        List<CoVo> coList = contentService.selectSiteCoList(keywords);
        return ResponseUtil.ok(coList);
    }

    /**
     * 获取站点信息
     */
    @GetMapping("/website")
    public Object website() {
        SiteSetting siteSetting = contentService.seeSiteSetting();
        if(siteSetting == null) {
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok(siteSetting);
    }

    /**
     * 获取轮播列表
     */
    @GetMapping("/carousellist")
    public Object carousellist(@RequestParam(defaultValue = "") String keywords,
                               @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer limit) {
        List<CarouselVo> coList = contentService.selectSiteCarouselList(keywords);
        return ResponseUtil.ok(coList);
    }

}
